package gitlet;

import java.io.*;

import static gitlet.Utils.sha1;

public class Blob implements Serializable {
    private byte[] data;

    public Blob(byte[] data) {
        this.data = data;
    }

    //在文件里读文件并且变成一个blob object，也就是一个字节数组。里面存的是字节
    public static Blob fromFile(String filePath) {
        return new Blob(Utils.readContents(new File(Repository.GITLET_DIR_OBJECT_BLOBS, filePath)));
    }

    public int length() {
        return data.length;
    }

    public byte[] getData() {
        return data;
    }

    //保存文件，并且把文件写入
    public void saveBlob() {
        String location = sha1(this.getData());
        File s = new File(Repository.GITLET_DIR_OBJECT_BLOBS, location);
        if (!s.exists()) {
            try {
                s.createNewFile();
            } catch (IOException var4) {
                var4.printStackTrace();
            }
        }
        Utils.writeContents(s, this.getData());

    }

    public String getSha1() {
        return Utils.sha1(this.getData());
    }


}
